#pragma once

#include "type.h"
#include "plane.h"

class TiXmlElement;
class TiXmlNode;

namespace ChunkSystem
{
	class ChunkBoundary;
	class Chunk;
	//Port is important also for loading chain.
	class Portal
	{
	public:

		typedef std::vector< Common::Vector2 > PortalOutline;
		typedef PortalOutline::iterator ItrPortalOutline;

		enum PortalType
		{
			eInvasive,
			eExternal,
		};

		Portal(TiXmlElement* pPortalData, ChunkBoundary* pOwnerBoundary);
		~Portal(void);

		Chunk* GetBindChunk(void) const;
		void BindChunk(Chunk* pTargetChunk);

		PortalType GetPortalType( void ) const;
		void SetPortalType( PortalType eType );

		Common::Vector3& GetLocalCenter(void);
		PortalOutline& GetOutLine(void);
		String GetLabel(void) const;

		ChunkBoundary* GetOwnerBoundary( void ) const;
		Common::Vector3 GetAxisU( void ) const;
		Common::Vector3 GetAxisV( void ) const;

	private:

		Chunk*				m_pBindChunk;
		
		Common::Vector3		m_uAxis;		// in local space
		Common::Vector3		m_vAxis;		// in local space
		Common::Vector3		m_origin;		// in local space
		Common::Vector3		m_localCentre;	// in local space
		Common::Vector3		m_centre;		// in world space
		
		PortalOutline		m_conPortalPoints;

		String				m_label;
		PortalType			m_ePortalType;
		
		ChunkBoundary*		m_pOwnerBoundary;
	};

	class ChunkBoundary
	{
	public:

		typedef std::vector<Portal*> Portals;

		ChunkBoundary( Chunk* pOwnerChunk );
		~ChunkBoundary(void);

		bool Load( TiXmlNode* pChunkBoundaryData );

		const Common::Plane& GetPlane( void )const{ return m_BoundaryPlane; }
		void GeneratePortals( void );

		Portals& GetBoundPortals(void) { return m_boundPortals; }
		Portals& GetUnboundPortals(void) { return m_unboundPortals; }

		void BindPortal( size_t iBindPortalIdx );
		void UnbindPortal( size_t iUnbindPortalIdx );

		Chunk* GetOwnerChunk(void);

	private:

		Chunk*			m_pOwnerChunk;

		Common::Plane	m_BoundaryPlane;
		Portals			m_boundPortals;
		Portals			m_unboundPortals;
	};

}